package servlet.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import servlet.bean.CourseBean;
import servlet.bean.TeacherBean;
import servlet.model.ServiceFactory;
import servlet.publicUtils.MyUtils;

public class TeaInfoModifyServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//request.setCharacterEncoding("UTF-8");
		String strOperationType = request.getParameter("opt");
		int iOpt = MyUtils.ObjtoInt(strOperationType);
		
		
		CourseBean couB = (CourseBean) MyUtils.getNewInstance(CourseBean.class);
		couB.setCou_name(request.getParameter("stucourse"));
		List<CourseBean> lstCou=ServiceFactory.getUserInfoManagerServiceInstance().getCou();
		request.getSession().setAttribute("lstCou", lstCou);
		//判断是否进入哪个界面
		String strJumpPage = "logined/teaInfoQuery.jsp";
		if(iOpt==3){
			// 准备用户进行修改的数据
			// 获取id主键值
			String strUid = request.getParameter("uid");
			TeacherBean teaB = ServiceFactory.getUserInfoManagerServiceInstance().getStuInfoById(strUid);
			// 将数据缓存到session对象中
			request.setAttribute("teaB", teaB);
			strJumpPage = "logined/teaadd.jsp";
			
		}else if(iOpt==1){
			//删除
		int stuId = MyUtils.ObjtoInt(request.getParameter("uid"));
			// 数据库删除

		int i = ServiceFactory.getUserInfoManagerServiceInstance().deleteStuById(stuId);
			// 跳转页面

		strJumpPage = "TeaInfoModifyServlet?opt=2";

		}else if(iOpt==2){
			//查询
			TeacherBean teaB = (TeacherBean) MyUtils.getNewInstance(TeacherBean.class);
			//CourseBean couB = (CourseBean) MyUtils.getNewInstance(CourseBean.class);
			//couB.setCou_name(request.getParameter("stucourse"));
			teaB.setStu_id(request.getParameter("stuid"));
			teaB.setStu_name(request.getParameter("stuname"));
			teaB.setStu_course(request.getParameter("stucourse"));
			teaB.setStu_record(MyUtils.ObjtoInt(request.getParameter("sturecord")));
			teaB.setStu_record2(MyUtils.ObjtoInt(request.getParameter("sturecord2")));
			
			//查询操作
			List<TeacherBean> lstStu=ServiceFactory.getUserInfoManagerServiceInstance().queryStuInfo(teaB);
			//List<CourseBean> lstCou=ServiceFactory.getUserInfoManagerServiceInstance().getCou();
			//获取lstStu.size() list的长度 ,传给lstStulenth
			request.getSession().setAttribute("lstStulenth", lstStu.size());
			request.getSession().setAttribute("lstStu", lstStu);
			//request.getSession().setAttribute("lstCou", lstCou);
			
			strJumpPage = "logined/teaInfoAddDetail.jsp";
		
		}else if(iOpt==4){
			//课程管理——编辑
			String strCouid = request.getParameter("uid");
			CourseBean coueditB = ServiceFactory.getUserInfoManagerServiceInstance().getCouInfoById(strCouid);
			// 将数据缓存到session对象中
			request.setAttribute("coueditB", coueditB);
			strJumpPage = "logined/teacouadd.jsp";
			
			
		}else if(iOpt==5){
			//课程管理——删除
		int couId = MyUtils.ObjtoInt(request.getParameter("uid"));
			// 数据库删除

		int i = ServiceFactory.getUserInfoManagerServiceInstance().deleteCouById(couId);
			// 跳转页面

		strJumpPage = "CouTempServlet?opt=0";
		}
		
		// 跳转到修改页面
		request.getRequestDispatcher(strJumpPage).forward(request, response);
		request.removeAttribute("teaB");
	}

}
